//
//  NewsTableView.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQNewsTableView.h"

@implementation NewsTableView



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsModel *model = self.Array[indexPath.row];
    ZDQNewMore *more = [[ZDQNewMore alloc] init];
    [more setValuesForKeysWithDictionary:model.style];
    
    if (more.images.count != 0) {
        return 150 * self.frame.size.height / 667;
        
    } else {
    
    return 100 * self.frame.size.height / 667;
        
    }
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.Array.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsModel *model = self.Array[indexPath.row];
    ZDQNewMore *more = [[ZDQNewMore alloc] init];
    [more setValuesForKeysWithDictionary:model.style];
    
    if (more.images.count == 0) {
        
        if (model.thumbnail.length == 0) {
            static NSString *reuse = @"Typereuse";
            ZDQMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[ZDQMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            }
            
            cell.titleLabel.text = model.title;
            cell.websideLabel.text = model.source;
            cell.backgroundColor = [UIColor clearColor];
            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.websideLabel.textColor = [UIColor whiteColor];
                if ([model.type isEqualToString:@"topic2"]) {
                    cell.timeLabel.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
                    cell.timeLabel.textColor = [UIColor whiteColor];
                    cell.timeLabel.text = @"专题";
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                } else {
                    
                    cell.timeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
                    cell.timeLabel.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
                    cell.timeLabel.textColor = [UIColor whiteColor];
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                    
                }

            } else {
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.websideLabel.textColor = [UIColor blackColor];
                if ([model.type isEqualToString:@"topic2"]) {
                    cell.timeLabel.backgroundColor = [UIColor redColor];
                    cell.timeLabel.textColor = [UIColor whiteColor];
                    cell.timeLabel.text = @"专题";
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                } else {
                    
                    cell.timeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
                    cell.timeLabel.backgroundColor = [UIColor whiteColor];
                    cell.timeLabel.textColor = [UIColor blackColor];
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                    
                }

            }
            
            
            return cell;
            
        } else {

        
        static NSString *reuse = @"reuse";
        RootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            
            cell = [[RootTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            
            
        }
        
        
        
        cell.titleLabel.text = model.title;
        [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];
            
        if (model.commentsall.length == 0) {
            
            cell.readLabel.text = @"0阅";
            
        } else {
        
            NSString *str = [NSString stringWithFormat:@"%@阅", model.commentsall];
        
            cell.readLabel.text = str;
        
        }
            cell.backgroundColor = [UIColor clearColor];
            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.readLabel.textColor = [UIColor whiteColor];
                cell.photoImageView.alpha = 0.5;
                if ([model.type isEqualToString:@"topic2"]) {
                    cell.timeLabel.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
                    cell.timeLabel.textColor = [UIColor redColor];
                    cell.timeLabel.text = @"专题";
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                } else {
                    
                    cell.timeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
                    cell.timeLabel.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
                    cell.timeLabel.textColor = [UIColor whiteColor];
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                    
                }
                
                } else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.readLabel.textColor = [UIColor blackColor];
                cell.photoImageView.alpha = 1;
                
                if ([model.type isEqualToString:@"topic2"]) {
                    cell.timeLabel.backgroundColor = [UIColor redColor];
                    cell.timeLabel.textColor = [UIColor whiteColor];
                    cell.timeLabel.text = @"专题";
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                } else {
                    
                    cell.timeLabel.text = [model.updateTime substringWithRange:NSMakeRange(11, 5)];
                    cell.timeLabel.backgroundColor = [UIColor whiteColor];
                    cell.timeLabel.textColor = [UIColor blackColor];
                    cell.timeLabel.textAlignment = NSTextAlignmentCenter;
                    
                }

                
            }
        
        return cell;
        }
        
    } else {

        static NSString *reuse = @"TABLEREUSE";
        NewsRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            
            cell = [[NewsRootTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            
            
        }
        
        cell.titleLabel.text = model.title;
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:more.images[0]]placeholderImage:[UIImage imageNamed:@"pac.png"]];
        [cell.centerImageView sd_setImageWithURL:[NSURL URLWithString:more.images[1]]placeholderImage:[UIImage imageNamed:@"pac.png"]];
        if (more.images.count == 3) {
            [cell.rightImageView sd_setImageWithURL:[NSURL URLWithString:more.images[2]]placeholderImage:[UIImage imageNamed:@"pac.png"]];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        if ([ZDQSingleton shareSingleton].isLongin == YES) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.leftImageView.alpha = 0.5;
            cell.rightImageView.alpha = 0.5;
            cell.centerImageView.alpha = 0.5;
        } else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.leftImageView.alpha = 1;
            cell.rightImageView.alpha = 1;
            cell.centerImageView.alpha = 1;
        }
        return cell;
        
        
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsModel *model = self.Array[indexPath.row];
    if ([model.type isEqualToString:@"sports_live"]) {
        
    } else {
    
    [self.iddelegate didSelectRowAtIndexPath:indexPath tableView:tableView];
    }
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
